package com.vshop.utils.date;

import com.vshop.config.dto.StringOutputConstant;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间以某种格式输入的工具类
 *
 * @author lm
 * @date 2020/8/11 14:04
 */
@Component
@Slf4j
public class DateUtil {
    /**
     * 根据套餐类型和订购周期计算出套餐预计到期时间
     * <p>
     * 只是理论上的到期时间
     * <p>
     * 格式：yyyy-MM-dd HH:mm:ss
     *
     * @param packageType
     * @param orderPeriod
     * @return
     */
    public static String deadlineDate(String packageType, String orderPeriod) {
        Calendar calendar = Calendar.getInstance();
        if ("0".equals(packageType) || "1".equals(packageType)) {
            calendar.add(Calendar.DATE, Integer.parseInt(orderPeriod));
        } else if ("2".equals(packageType)) {
            calendar.add(Calendar.MONTH, Integer.parseInt(orderPeriod));
        } else if ("3".equals(packageType)) {
            calendar.add(Calendar.MONTH, Integer.parseInt(orderPeriod) * 3);
        } else if ("4".equals(packageType)) {
            calendar.add(Calendar.MONTH, Integer.parseInt(orderPeriod) * 6);
        } else if ("5".equals(packageType)) {
            calendar.add(Calendar.YEAR, Integer.parseInt(orderPeriod));
        }
        Date time = calendar.getTime();
        String deadline = new SimpleDateFormat("yyyy-MM-dd").format(time) + " 23:59:59";
        log.info("到期时间：" + deadline);
        return deadline;
    }

    /**
     * 获取当前日期
     * <p>
     * 可以作为订购时间
     * <p>
     * 同时也是开始激活时间
     * <p>
     * 格式：yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String currentDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentDate = sdf.format(date);
        log.info("开始日期：" + currentDate);
        return currentDate;
    }

    /**
     * 正在使用订单：
     * <p>
     * 计算从今天到理论到期时间之间是多少天
     *
     * @param date
     * @return
     */
    public static String remainingDays(Date date, String type) {
        String remainingDays;
        Date nowDays = new Date();
        //1.日期格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date futureDate;
        if (StringOutputConstant.CODE_ONE.equals(type)) {
            try {
                //2.未来某天的日期
                futureDate = sdf.parse(new SimpleDateFormat("yyyy-MM-dd").format(date) + " 23:59:59");
                //3.输出间隔天数getTime获取的是毫秒:未来时间-此刻时间
                remainingDays = (futureDate.getTime() - nowDays.getTime()) / 24 / 60 / 60 / 1000 + "";
            } catch (ParseException e) {
                remainingDays = "0";
            }
        } else if (StringOutputConstant.CODE_TWO.equals(type)) {
            try {
                //2.未来某天的日期
                futureDate = sdf.parse(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(date));
                //3.输出间隔天数getTime获取的是毫秒:未来时间-此刻时间
                remainingDays = (futureDate.getTime() - nowDays.getTime()) / 24 / 60 / 60 / 1000 + "";
            } catch (ParseException e) {
                remainingDays = "0";
            }
        } else {
            remainingDays = "0";
        }
        return remainingDays;
    }

    /**
     * 未启用订单：
     * <p>
     * 计算订单理论的剩余天数
     *
     * @param orderPeriod
     * @param packageType
     * @return
     */
    public static String theoreticalDays(String orderPeriod, String packageType) {
        String theoreticalDays;
        if (StringOutputConstant.CODE_ZERO.equals(packageType) || StringOutputConstant.CODE_ONE.equals(packageType)) {
            theoreticalDays = orderPeriod;
        } else if (StringOutputConstant.CODE_TWO.equals(packageType)) {
            /*小余12的就直接*30就完了*/
            if (Integer.parseInt(orderPeriod) < StringOutputConstant.TWELVE) {
                theoreticalDays = String.valueOf(Integer.parseInt(orderPeriod) * 30);
            } else {
                /*周期除以12取商*/
                int multiple = Integer.parseInt(orderPeriod) / 12;
                /*周期模12取余数*/
                int remainder = Integer.parseInt(orderPeriod) % 12;
                /*商*365+余数*30*/
                theoreticalDays = String.valueOf(multiple * 365 + remainder * 30);
            }
        } else if (StringOutputConstant.CODE_THREE.equals(packageType)) {
            theoreticalDays = String.valueOf(Integer.parseInt(orderPeriod) * 90);
        } else if (StringOutputConstant.CODE_FOUR.equals(packageType)) {
            theoreticalDays = String.valueOf(Integer.parseInt(orderPeriod) * 180);
        } else if (StringOutputConstant.CODE_FIVE.equals(packageType)) {
            theoreticalDays = String.valueOf(Integer.parseInt(orderPeriod) * 365);
        } else {
            theoreticalDays = "0";
        }
        return theoreticalDays;
    }

    public static void main(String args[]) {
        Integer a = 25;
        int multiple = a / 12;
        int remainder = a % 12;
        System.out.println("商：" + multiple + ";余数：" + remainder + "；天数：" + String.valueOf(multiple * 365 + remainder * 30));

        // 获取今天的日期
        LocalDate today = LocalDate.now();
        // 今天是几号
        int dayofMonth = today.getDayOfMonth();
        // 今天是周几（返回的是个枚举类型，需要再getValue()）
        int dayofWeek = today.getDayOfWeek().getValue();
        // 取本月最后一天，再也不用计算是28，29，30还是31：
        LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        // 取下一天：
        LocalDate firstDayOfNextMonth = lastDayOfThisMonth.plusMonths(12);
        LocalDate end = today.plus(12, ChronoUnit.MONTHS);
        /*取两个日期之前相差多少天*/
        long days = ChronoUnit.DAYS.between(today, end);
        System.out.println("~~~~~~" + today + "=====" + lastDayOfThisMonth + "++++++" + firstDayOfNextMonth + "*******" + end + "------" + days);

        /*一周从周日开始*/
        WeekFields weekFields = WeekFields.of(DayOfWeek.SUNDAY, 7);
        /*一周从周一开始*/
        WeekFields weekFields1 = WeekFields.of(DayOfWeek.MONDAY, 1);
        LocalDate now = LocalDate.of(2021, 7, 19);

        System.out.println(now + "周日开始一周算法：" + now.get(weekFields.weekOfYear()) + "周");
        System.out.println(now + "周一开始一周算法：" + now.get(weekFields1.weekOfYear()) + "周");
    }
}
